require.config({
    baseUrl: '../js/lib',
    paths: {
        jquery: 'jquery',
        axios: 'axios',
        cookie: 'cookie'
    },
    shim: {
    }
});



require(['jquery'], function ($) {
    const url = ['./template/login.html', './template/register.html']
    $(function () {
        $('.lorr>span').on('click', function () {

            let index = $('.lorr>span').index(this);
            console.log($(this).hasClass('active'));
            if ($(this).hasClass('active')) return;
            $('.lorr>span').removeClass('active');
            $(this).addClass('active');

            if (index == 0) {
                $('.login').animate({
                    left: '30px'
                })
                $('.register').animate({
                    left: '460px'
                })
                $('.lorr>i').animate({
                    left: "0px"
                })
            } else {
                $('.login').animate({
                    left: '-460px'
                })
                $('.register').animate({
                    left: '30px'
                })
                $('.lorr>i').animate({
                    left: "70px"
                })
            }
        })
    })
})

require(['jquery'], function ($) {
    $(function () {

        $('.template input').on('focus', function () {

            $(this).prev().css('fontSize', '6px').css('top', '25px');
        });
        $('.template input').on('blur', function () {

            if (!$(this).val()) {
                $(this).prev().css('fontSize', '14px').css('top', '40px').css('color', 'red');
                $(this).css('box-shadow', '0 0 3px 3px #ff5c00').css('background-color', 'rgba(255, 0, 0, 0.137)');

            } else {
                $(this).css('box-shadow', 'none').css('background-color', '#f9f9f9');
                $(this).prev().css('color', 'black')
            }
        });
    })
})






require(['jquery', 'axios', 'cookie'], function ($, axios, cookie) {


    $('.getver').on('click', function () {

        axios.get('../users/getcode', { params: { phone: $('#phone').val() } }).then(res => {
            console.log(res);
        }).catch(err => {
            console.log(err);
        })
    })

    $('.register-btn').on('click', function () {

        let data = {
            'phone': $('#phone').val(),
            'code': $('#yanzheng').val(),
            'password': $('#respassword').val()
        };
        console.log(data);
        axios.post('../users/register', data).then(res => {
            console.log(res.data.success);
            if (res.data.success) {
                alert('注册成功');
                $('.login').animate({
                    left: '30px'
                })
                $('.register').animate({
                    left: '460px'
                })
                $('.lorr>i').animate({
                    left: "0px"
                })
            } else {
                alert('注册失败')
            }
        }).catch(err => {
            console.log(err);
        })
    })

    $('.login-btn').on('click', function () {
        let data = {
            'phone': $('#username').val(),
            'password': $('#password').val()
        };
        axios.post('../users/login', data).then(res => {

            let token = res.data.token;
            if (res.data.success) {
                window.location.href = 'http://localhost:3000/html/index.html';
                alert('登录成功')
            } else {
                alert('登录失败')
            }
            localStorage.setItem('token', token);



        }).catch(err => {
            console.log(err);
        })
    })

    require(['jquery', 'axios'], function ($, axios) {

        let password = document.querySelector('#respassword');
        let phone = document.querySelector('#phone')
        let btn = document.querySelector('.getver');


        password.onblur = function () {
            let reg = /^[\d\w]{6,12}$/;
            if (reg.test(this.value)) {
                this.nextElementSibling.innerHTML = "密码格式正确";

            } else {
                this.nextElementSibling.innerHTML = "请输入6~12以内数字、字母或组合密码";
            }
        }

        var time = 60
        btn.onclick = function () {
            if ($('.getver button').html() != "获取验证码") {
                $('.getver button').attr('disabled', 'false');
                return;
            } else {
                $('.getver button').attr('disabled', 'true');
            }

            let reg2 = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/

            var name = phone.value;
            if (name == "") {
                this.nextElementSibling.innerHTML = '手机号不能为空！';
                return;
            } else if (!reg2.test(name)) {
                this.nextElementSibling.innerHTML = '手机格式不正确';
                return;
            }
            else {
                this.nextElementSibling.innerHTML = '验证码发送成功';
            }
            var timer = setInterval(function () {
                time--;
                btn.innerHTML = time + "秒";

                if (time == 0) {
                    time = 60;
                    clearInterval(timer);
                    btn.innerHTML = "获取验证码";

                }
            }, 1000)

        }

    });


})

require(['jquery', 'axios'], function ($, axios) {

    let password = document.querySelector('#respassword');
    let phone = document.querySelector('#phone')
    let btn = document.querySelector('.getver');
    let flag1 = false;
    let flag2 = false;

    password.onblur = function () {
        let reg = /^[\d\w]{6,12}$/;
        if (reg.test(this.value)) {
            this.nextElementSibling.innerHTML = "密码格式正确";
            flag1 = true;
        } else {
            this.nextElementSibling.innerHTML = "请输入6~12以内数字、字母或组合密码";
            flag1 = false;
        }
    }

    var time = 60
    btn.onclick = function () {

        let reg2 = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/

        var name = phone.value;
        if (name == "") {
            this.nextElementSibling.innerHTML = '手机号不能为空！';
            flag2 = false;
            return;
        } else if (!reg2.test(name)) {
            this.nextElementSibling.innerHTML = '手机格式不正确';
            flag2 = false;
            return;
        }
        else {
            this.nextElementSibling.innerHTML = '验证码发送成功';
            flag2 = true;
        }

        var timer = setInterval(function () {
            time--;
            btn.innerHTML = time + "秒";
            btn.ariaDisabled = true;
            if (time == 0) {
                time = 60;
                clearInterval(timer);
                btn.innerHTML = "获取验证码";
                btn.disabled = false;
            }
        }, 1000)

    }

});

